<?php
/**
 * Created by PhpStorm.
 * User: lyz
 * Date: 2019/7/19
 * Time: 9:48
 * 每天23:58:00更新货币的最后行情
 */


define('APP_PATH', __DIR__ . '/application/');
// 加载框架引导文件
require __DIR__ . '/thinkphp/start.php';

function price(){

    echo "\r\n开始时间：".date('Y-m-d H:i:s')."\r\n";

    $time=time();
    $ty = date('Y', time());
    $tm = date('m', time());
    $td = date('d', time());
    $todayStartTime = mktime(0,0,0,$tm,$td,$ty);
    $todayEndTime   = mktime(23,59,59,$tm,$td,$ty);

    $btcData=\db('currency_price')->where('symbol',"BTC")->where('type',"day")->whereBetween('createtime',[$todayStartTime,$todayEndTime])->find();
    $ethData=\db('currency_price')->where('symbol',"ETH")->where('type',"day")->whereBetween('createtime',[$todayStartTime,$todayEndTime])->find();
    $bchData=\db('currency_price')->where('symbol',"BCH")->where('type',"day")->whereBetween('createtime',[$todayStartTime,$todayEndTime])->find();



    $getPrice=new \app\common\core\Wallet("aa","aa","aa");
    $btcResult=$getPrice->get("https://api.coinbase.com/v2/prices/BTC-USD/sell");
    $btc=$btcResult['data']['amount'];
    $ethResult=$getPrice->get("https://api.coinbase.com/v2/prices/ETH-USD/sell");
    $eth=$ethResult['data']['amount'];
    $bchResult=$getPrice->get("https://api.coinbase.com/v2/prices/BCH-USD/sell");
    $bch=$bchResult['data']['amount'];

    if(!$btcData){
        db('currency_price')->insert([
            "symbol"=>"BTC",
            "name"=>"bitcoin",
            "price"=>$btc,
            "type"=>"day",
            "createtime"=>time()
            ]);
    }
    if(!$ethData){
        db('currency_price')->insert([
            "symbol"=>"ETH",
            "name"=>"ethereum",
            "price"=>$eth,
            "type"=>"day",
            "createtime"=>time()
        ]);
    }

    if(!$bchData){
        db('currency_price')->insert([
            "symbol"=>"BCH",
            "name"=>"bitcoin-cash",
            "price"=>$bch,
            "type"=>"day",
            "createtime"=>time()
        ]);
    }
    echo "执行结束时间：".date('Y-m-d H:i:s')."\r\n";
}

price();